package com.jms.tomcat.http;

import com.jms.tomcat.core.JmsServletContext;
import com.jms.tomcat.util.StaticResourceUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JmsRequestDispatcher implements RequestDispatcher {

    /**
     * 转发的路径
     */
    private String path;

    public JmsRequestDispatcher(String path) {
        this.path = path;
    }

    public JmsRequestDispatcher() {}

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        JmsServletContext servletContext = (JmsServletContext) request.getServletContext();
        //查找转发路径对应的servlet
        Servlet servlet = servletContext.getServletByPath(path);
        if (servlet != null) {
            //找到servlet则调用servlet的service方法
            servlet.service(request, response);
        } else {
            //找不到servlet则去找静态资源
            StaticResourceUtils.getStaticResource((HttpServletResponse) response, path);
        }
    }

    @Override
    public void include(ServletRequest request, ServletResponse response) {

    }
}
